package com.yr.controller;

import com.yr.common.util.PageResult;
import com.yr.common.util.R;
import com.yr.entity.Student;
import com.yr.service.IStudentService;
import com.yr.vo.studentvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yr
 * @since 2025-01-07
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService studentService;
    @RequestMapping("/list")
    public R getStudent(){
        return R.ok().data("list",studentService.list());
    }
    //分页
    @GetMapping("/findbyPage/{page}/{pagesize}")
    public R findbyPage(@PathVariable("page") Integer page,@PathVariable("pagesize") Integer pagesize){
        PageResult<Student> pageResult = studentService.findbyPage(page,pagesize);
        return R.ok().data("pageResult",pageResult);
    }
    @PostMapping("/search/{page}/{pagesize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) studentvo vo){
        PageResult<Student> pageResult = studentService.search(page,pageSize,vo);
        return R.ok().data("pageResult",pageResult);
    }
    @PostMapping("/add")
    public R add(@RequestBody Student student){
        boolean flag = studentService.add(student);
        return flag ? R.ok().message("添加成功") : R.error().message("添加失败");
    }
    @PutMapping("/update")
    public R update(@RequestBody Student student){
        boolean flag = studentService.updateById(student);
        return flag ? R.ok().message("修改成功!") : R.error().message("修改失败");
    }
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return studentService.removeByIds(sids) ? R.ok().message("删除成功") : R.error().message("删除失败");
    }
}
